package com.hdit.dao.impl;

import java.util.Date;
import java.util.List;

import com.hdit.bean.Pager;
import com.hdit.dao.AuctionDao;
import com.hdit.entity.Admin;
import com.hdit.entity.Auction;
import com.hdit.entity.Member;
import com.hdit.util.SystemConfigUtil;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

/**
 * Dao实现类 - 商品
 * ============================================================================
 
 
027556F67EB15567DE4DCC7E9D2E5516
 * ============================================================================
 */

@Repository
public class AuctionDaoImpl extends BaseDaoImpl<Auction, String> implements AuctionDao {
	
	
	
	@SuppressWarnings("unchecked")
	public List<Auction> getAuctionList(Auction auction) {
		String hql = "from Auction as auction where auction.auction.id = ? order by auction.createDate desc";
		return getSession().createQuery(hql).setParameter(0, auction.getId()).list();
	}
	
	@SuppressWarnings("unchecked")
	public List<Auction> getAuctionList(int firstResult, int maxResults) {
		String hql = "from Auction as auction where auction.isMarketable = ? order by auction.createDate desc";
		return getSession().createQuery(hql).setParameter(0, true).setFirstResult(firstResult).setMaxResults(maxResults).list();
	}

	@SuppressWarnings("unchecked")
	public List<Auction> getAuctionList(Date beginDate, Date endDate, int firstResult, int maxResults) {
		if (beginDate != null && endDate == null) {
			String hql = "from Auction as auction where auction.isMarketable = ? and auction.createDate > ? order by auction.createDate desc";
			return getSession().createQuery(hql).setParameter(0, true).setParameter(1, beginDate).setFirstResult(firstResult).setMaxResults(maxResults).list();
		} else if (endDate != null && beginDate == null) {
			String hql = "from Auction as auction where auction.isMarketable = ? and auction.createDate < ? order by auction.createDate desc";
			return getSession().createQuery(hql).setParameter(0, true).setParameter(1, endDate).setFirstResult(firstResult).setMaxResults(maxResults).list();
		} else if (endDate != null && beginDate != null) {
			String hql = "from Auction as auction where auction.isMarketable = ? and auction.createDate > ? and auction.createDate < ? order by auction.createDate desc";
			return getSession().createQuery(hql).setParameter(0, true).setParameter(1, beginDate).setParameter(2, endDate).setFirstResult(firstResult).setMaxResults(maxResults).list();
		} else {
			String hql = "from Auction as auction where auction.isMarketable = ? order by auction.createDate desc";
			return getSession().createQuery(hql).setParameter(0, true).setFirstResult(firstResult).setMaxResults(maxResults).list();
		}
	}


	@SuppressWarnings("unchecked")
	public List<Auction> getBestAuctionList(int maxResults) {
		String hql = "from Auction as auction where auction.isMarketable = ? and auction.isBest = ? order by auction.createDate desc";
		return getSession().createQuery(hql).setParameter(0, true).setParameter(1, true).list();
	}



	@SuppressWarnings("unchecked")
	public List<Auction> getHotAuctionList(int maxResults) {
		String hql = "from Auction as auction where auction.isMarketable = ? and auction.isHot = ? order by auction.createDate desc";
		return getSession().createQuery(hql).setParameter(0, true).setParameter(1, true).list();
	}

	
	@SuppressWarnings("unchecked")
	public List<Auction> getNewAuctionList(int maxResults) {
		String hql = "from Auction as auction where auction.isMarketable = ? and auction.isNew = ? order by auction.createDate desc";
		return getSession().createQuery(hql).setParameter(0, true).setParameter(1, true).list();
	}

	
	public Pager getFavoriteAuctionPager(Member member, Pager pager) {
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Auction.class);
		detachedCriteria.createAlias("favoriteMemberSet", "favoriteMemberSet");
		detachedCriteria.add(Restrictions.eq("favoriteMemberSet.id", member.getId()));
		detachedCriteria.addOrder(Order.desc("name"));
		return findByPager(pager, detachedCriteria);
	}
	
	public Long getStoreAlertCount() {
		String hql = "select count(*) from Auction as auction where auction.isMarketable = ? and auction.store - auction.freezeStore <= ?";
		return (Long) getSession().createQuery(hql).setParameter(0, true).setParameter(1, SystemConfigUtil.getSystemConfig().getStoreAlertCount()).uniqueResult();
	}
	
	public Long getMarketableAuctionCount() {
		String hql = "select count(*) from Auction as auction where auction.isMarketable = ?";
		return (Long) getSession().createQuery(hql).setParameter(0, true).uniqueResult();
	}
	
	public Long getUnMarketableAuctionCount() {
		String hql = "select count(*) from Auction as auction where auction.isMarketable = ?";
		return (Long) getSession().createQuery(hql).setParameter(0, false).uniqueResult();
	}
	
	// 关联处理
	@Override
	public void delete(Auction auction) {
		super.delete(auction);
	}

	// 关联处理
	@Override
	public void delete(String id) {
		Auction auction = load(id);
		this.delete(auction);
	}

	// 关联处理
	@Override
	public void delete(String[] ids) {
		for (String id : ids) {
			Auction auction = load(id);
			this.delete(auction);
		}
	}
	
	
	// 关联处理
	@Override
	public Auction getMaxAuctionPrice(String productid) {
		
		String hql = "from Auction as auction where auction.product.id = ?  order by auction.price desc";
		
		List list=getSession().createQuery(hql).setParameter(0, productid).setMaxResults(1).list();
		
		if(list.size()==0){
			return null;
		}
		else{
			
			return (Auction)list.get(0);
		}
		
		
	}

}